package com.hejia.alauda.service;

import com.hejia.alauda.entity.BankCard;
import com.hejia.alauda.entity.DayInventory;
import com.hejia.alauda.entity.Financing;
import com.hejia.alauda.entity.Order;
import com.hejia.alauda.entity.Order.Status;
import com.hejia.alauda.entity.PayGate;
import com.hejia.alauda.entity.Product;
import com.hejia.alauda.entity.example.FinancingExample;
import com.hejia.alauda.entity.example.OrderExample;
import com.hejia.alauda.entity.example.RepaymentOrderExample;
import com.hejia.alauda.entity.example.UserExample;
import com.hejia.alauda.persistence.mybatis.*;
import com.hejia.alauda.persistence.readonlymybatis.ReadonlyOrderMapper;
import com.hejia.alauda.utils.Pager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 *
 *
 * Created by showone on 2014 上午10:33:49.
 */
@Service
public class StatisticServiceImpl implements StatisticService {

	@Resource
	private ReadonlyOrderMapper readonlyOrderMapper;

	@Resource
	private UserMapper userMapper;

	@Resource
	private ProductMapper productMapper;

	@Resource
	private PayGateMapper payGateMapper;

	@Resource
	private BankCardMapper bankCardMapper;

	@Resource
	private FinancingMapper financingMapper;

	@Resource
	private RepaymentOrderMapper repaymentreadonlyOrderMapper;

	@Resource
	private DayInventoryMapper dayInventoryMapper;

	public Double dayTotalSales(String statDate) {
		return readonlyOrderMapper.dayTotalSales(statDate);
	}

	@Override
	public Map<Integer, Double> daySales(String statDate) {
		Map<Integer, Double> rest = new HashMap<Integer, Double>();
		List<Map<String, Object>> list = readonlyOrderMapper.daySales(statDate);

		for (int i = 0; list != null && i < list.size(); i++) {
			Map<String, Object> m = list.get(i);

			double amount = ((BigDecimal) m.get("amount")).doubleValue();
			rest.put((Integer) m.get("product_id"), amount);
		}
		return rest;
	}

	@Override
	public Double dayTotalSales(String statDate, Status status) {
		return readonlyOrderMapper.dayTotalSalesByStatus(statDate, status);
	}

	@Override
	public Map<Integer, Double> daySales(String statDate, Status status) {
		Map<Integer, Double> rest = new HashMap<Integer, Double>();
		List<Map<String, Object>> list = readonlyOrderMapper.daySalesByStatus(
				statDate, status);

		for (int i = 0; list != null && i < list.size(); i++) {
			Map<String, Object> m = list.get(i);

			double amount = ((BigDecimal) m.get("amount")).doubleValue();
			rest.put((Integer) m.get("product_id"), amount);
		}
		return rest;
	}

	@Override
	public int numberOfBuyer(String statDate) {
		return readonlyOrderMapper.numberOfBuyer(statDate);
	}

	@Override
	public int numberOfBuyer(String statDate, Status status) {
		return readonlyOrderMapper.numberOfBuyerByStatus(statDate, status);
	};

	@Override
	public int dayNumOfOrder(String statDate) {
		return readonlyOrderMapper.dayNumOfOrder(statDate);
	}

	@Override
	public int dayNumOfOrder(String statDate, Status status) {
		return readonlyOrderMapper.dayNumOfOrderByStatus(statDate, status);
	}

	@Override
	public Double manyDaysTotalSales(String statDate, String statDate2,
			Status status) {
		OrderExample exam = new OrderExample();
		Double num;
		if (status == null) {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);
		} else {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.sumByExample("amount", exam);
		return num;
	}

	@Override
	public int manyDaysNumOfOrder(String statDate, String statDate2,
			Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status == null) {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);

		} else {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.countByExample(exam);
		return num;
	}

	@Override
	public int manyDaysNumOfBuyer(String statDate, String statDate2,
			Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status == null) {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);

		} else {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<",
					statDate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					statDate2);
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.countColByExample("distinct user_id", exam);
		return num;
	}

	@Override
	public Double allDaysTotalSales(Status status) {
		OrderExample exam = new OrderExample();
		Double num;
		if (status != null) {
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.sumByExample("amount", exam);
		return num;
	}

	@Override
	public int allDaysNumOfBuyer(Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status != null) {
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.countColByExample("distinct user_id", exam);
		return num;
	}

	@Override
	public int allDaysNumOfOrder(Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status != null) {
			exam.appendCriterion("status=", status);
		}
		num = readonlyOrderMapper.countByExample(exam);
		return num;
	}

	@Override
	public Double productTotalSales(Long id, Status status) {
		OrderExample exam = new OrderExample();
		Double num;
		if (status != null) {
			exam.appendCriterion("product_id=", id);
			exam.appendCriterion("status=", status);
		} else {
			exam.appendCriterion("product_id=", id);
		}
		num = readonlyOrderMapper.sumByExample("amount", exam);
		return num;
	}

	@Override
	public int productNumOfBuyer(Long id, Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status != null) {
			exam.appendCriterion("product_id=", id);
			exam.appendCriterion("status=", status);
		} else {
			exam.appendCriterion("product_id=", id);
		}
		num = readonlyOrderMapper.countColByExample("distinct user_id", exam);
		return num;
	}

	@Override
	public int productNumOfOrder(Long id, Status status) {
		OrderExample exam = new OrderExample();
		Integer num;
		if (status != null) {
			exam.appendCriterion("product_id=", id);
			exam.appendCriterion("status=", status);
		} else {
			exam.appendCriterion("product_id=", id);
		}
		num = readonlyOrderMapper.countByExample(exam);
		return num;
	}

	@Override
	public Integer dayNumOfUser(String regdate) {
		return userMapper.dayNumOfUser(regdate);
	}

	@Override
	public Integer dayNumOfUser(String regdate, String manydate) {
		UserExample exam = new UserExample();
		exam.appendCriterion("str_to_date(createDate,'%Y-%m-%d')<=", regdate);
		exam.appendCriterion("str_to_date(createDate,'%Y-%m-%d')>=", manydate);
		Integer num = userMapper.countByExample(exam);
		return num;
	}

	public Integer numberOfUser() {
		return userMapper.numberOfUser();
	}

	@Override
	public int numOfPersons(String startdate, String enddate, String param) {
		OrderExample exam = new OrderExample();
		Integer num = null;
		if (param == "numOfBuyers") {
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<=",
					startdate);
			exam.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					enddate);
			exam.appendCriterion("status=", Order.Status.STATUS_PAID);
			num = readonlyOrderMapper.countColByExample("distinct user_id",
					exam);

		} else if (param == "newOfBuyers") {
			num = readonlyOrderMapper.newNumOfBuyerByDay(startdate, enddate);

		} else if (param == "registerBuyers") {
			num = readonlyOrderMapper.registerBuyers(startdate, enddate);

		} else if (param == "twiceBuyers") {
			num = readonlyOrderMapper.twiceBuyers(startdate, enddate);

		} else if (param == "redeemBuyers") {
			num = readonlyOrderMapper.redeemBuyers(startdate, enddate);

		} else if (param == "fifteenBuyers") {
			num = readonlyOrderMapper.fifteenBuyers(startdate, enddate);

		} else if (param == "alltwiceBuyers") {
			num = readonlyOrderMapper.alltwiceBuyers();

		} else if (param == "allredeemBuyers") {
			num = readonlyOrderMapper.allredeemBuyers();

		} else if (param == "allFifteenBuyers") {
			num = readonlyOrderMapper.allFifteenBuyers();

		} else {
			exam.appendCriterion("status=", Order.Status.STATUS_PAID);
			num = readonlyOrderMapper.countColByExample("distinct user_id",
					exam);

		}
		return num;
	}

	@Override
	public Double selectPerAmount(BigDecimal productPaid,
			Integer productPaidOrder) {
		Double perAmount;
		BigDecimal orderNumber = new BigDecimal(productPaidOrder);
		perAmount = productPaid
				.divide(orderNumber, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return perAmount;
	}

	@Override
	public Double incomeAndExpense(String startdate, String enddate,
			String param) {
		Double num;
		FinancingExample example = new FinancingExample();
		RepaymentOrderExample example2 = new RepaymentOrderExample();
		if (param == "income") {
			example.appendCriterion("str_to_date(createdate,'%Y-%m-%d')<=",
					startdate);
			example.appendCriterion("str_to_date(createdate,'%Y-%m-%d')>=",
					enddate);
			num = financingMapper.sumByExample("amount", example);
		} else {
			example2.appendCriterion("str_to_date(repaytime,'%Y-%m-%d')<=",
					startdate);
			example2.appendCriterion("str_to_date(repaytime,'%Y-%m-%d')>=",
					enddate);
			num = repaymentreadonlyOrderMapper.sumByExample("amount", example2);
		}
		return num;
	}

	@Override
	public Double allIncomeAndExpense(String param) {
		Double num;
		FinancingExample example = new FinancingExample();
		RepaymentOrderExample example2 = new RepaymentOrderExample();
		if (param == "allincome") {
			num = financingMapper.sumByExample("amount", example);
		} else if (param == "allexpenses") {
			example2.appendCriterion("repaytime!=", "");
			num = repaymentreadonlyOrderMapper.sumByExample("amount", example2);
		} else if (param == "expen") {
			num = financingMapper.selectExpend();
		} else {
			example.appendCriterion("status!=", "STATUS_PAYBACK");
			example.appendCriterion("status!=", "STATUS_REPAID");
			num = financingMapper.sumByExample("amount", example);
		}
		return num;
	}

	@Override
	public List<Map<String, Object>> productAnalyse(String startDate,
			String endDate, String index) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if ("times".equals(index)) {
			/*
			 * 交易次数
			 */
			list = readonlyOrderMapper.countTimes(startDate, endDate);
		} else if ("successtimes".equals(index)) {
			/*
			 * 成功交易次数
			 */
			list = readonlyOrderMapper.countSuccessTimes(startDate, endDate);
		} else if ("successbuyer".equals(index)) {
			/*
			 * 成功交易用户数
			 */
			list = readonlyOrderMapper.countSuccessBuyer(startDate, endDate);
		} else if ("buyer".equals(index)) {
			/*
			 * 交易用户数
			 */
			list = readonlyOrderMapper.countBuyer(startDate, endDate);
		} else {
			/*
			 * 交易金额
			 */
			list = readonlyOrderMapper.countProSales(startDate, endDate);
		}
		return list;
	}

	@Override
	public List<Product> allProductByDate(String statDate, String statDate2) {
		return productMapper.allProductByDate(statDate, statDate2);
	}

	@Override
	public List<PayGate> payGateByDate(String statDate, String statDate2,
			String param) {
		if ("all".equals(param)) {
			return payGateMapper.allPayGateByDate();
		} else {
			return payGateMapper.payGateByDate(statDate, statDate2);
		}
	}

	@Override
	public List<BankCard> bankCardByDate(String statDate, String statDate2,
			String param) {
		if ("all".equals(param)) {
			return bankCardMapper.allBankCardByDate();
		} else {
			return bankCardMapper.bankCardByDate(statDate, statDate2);
		}
	}

	@Override
	public List<Map<String, Object>> statInventoryByRegion() {
		return financingMapper.statInventoryByRegion();
	}

	@Override
	public List<Map<String, Object>> statInventoryInZheJiang() {
		return financingMapper.statInventoryInZheJiang();
	}

	@Override
	public List<Map<String, Object>> statInventoryByType() {
		return financingMapper.statInventoryByType();
	}

	@Override
	public List<Map<String, Object>> statByRegionLimit() {
		return financingMapper.statByRegionLimit();
	}

	@Override
	public List<Map<String, Object>> statInZheJiangLimit() {
		return financingMapper.statInZheJiangLimit();
	}

	@Override
	public List<Map<String, Object>> statInventory(String startdate,
			String enddate, String param) {
		List<Map<String, Object>> inventoryList = new ArrayList<Map<String, Object>>();
		if (param == "bymonth") {
			inventoryList = financingMapper.statInventoryByMonth(startdate,
					enddate);
		} else if (param == "byweek") {
			inventoryList = financingMapper.statInventoryByWeek(startdate,
					enddate);
		} else {
			inventoryList = financingMapper.statInventoryByYear(startdate,
					enddate);
		}
		return inventoryList;
	}

	@Override
	public List<Map<String, Object>> statByIntere(String startdate,
			String enddate, String param) {
		List<Map<String, Object>> intereList = new ArrayList<Map<String, Object>>();
		if (param == "bymonth") {
			intereList = financingMapper
					.statByIntereByMonth(startdate, enddate);
		} else if (param == "byweek") {
			intereList = financingMapper.statByIntereByWeek(startdate, enddate);
		} else {
			intereList = financingMapper.statByIntereByYear(startdate, enddate);
		}
		return intereList;
	}

	@Override
	public Map<String, Object> stattrendByMonth(String enddate) {
		/*FinancingExample example = new FinancingExample();
		example.appendCriterion(" (status='STATUS_INTEREST' or status='STATUS_DEFAULT') ");
		example.appendCriterion(" repaymentTime < ", enddate);
		Double amount = financingMapper.sumByExample("amount", example);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("inven", amount);
		return result;*/
		return financingMapper.stattrendByMonth(enddate);
	}

	@Override
	public Double stattrendByDate(String enddate) {
		return dayInventoryMapper.selectInventoryByDate(enddate);
	}

	@Override
	public List<Map<String, Object>> inventoryByDay(String startdate,
			String enddate) {
		return dayInventoryMapper.inventoryByDay(startdate, enddate);
	}

	@Override
	public List<Map<String, Object>> selectSalesByDate(String startdate,
			String enddate) {
		return dayInventoryMapper.selectSalesByDate(startdate, enddate);
	}

	@Override
	public List<Map<String, Object>> salesStatus(String param) {
		List<Map<String, Object>> salesList = new ArrayList<Map<String, Object>>();
		if ("rate".equals(param)) {
			salesList = financingMapper.salesByRate();
		} else if ("region".equals(param)) {
			salesList = financingMapper.salesByRegion();
		}
		return salesList;
	}

	@Override
	public List<Map<String, Object>> salesConsist(String startdate,
			String enddate, String param) {
		List<Map<String, Object>> salesList = new ArrayList<Map<String, Object>>();
		if ("month".equals(param)) {
			salesList = financingMapper.salesByMonth(startdate, enddate);
		} else if ("week".equals(param)) {
			salesList = financingMapper.salesByWeek(startdate, enddate);
		} else {
			salesList = financingMapper.salesByYear(startdate, enddate);
		}
		return salesList;
	}

	@Override
	public Double selectIntoDayinventory(String statdate, String param) {
		RepaymentOrderExample rexam = new RepaymentOrderExample();
		FinancingExample fexam = new FinancingExample();
		Double amount = (double) 0;
		if ("payment".equals(param)) {
			rexam.appendCriterion("str_to_date(repaytime,'%Y-%m-%d')=",
					statdate);
			amount = repaymentreadonlyOrderMapper.sumByExample("amount", rexam);
		} else if ("capital".equals(param)) {
			amount = financingMapper.selectPaymentByDay(statdate);
		} else {
			fexam.appendCriterion("str_to_date(createDate,'%Y-%m-%d')=",
					statdate);
			amount = financingMapper.sumByExample("amount", fexam);
		}
		return amount;
	}

	@Override
	public Double selectSalesByTime(String statdate, String param) {
		Double amount = (double) 0;
		FinancingExample fexam = new FinancingExample();
		fexam.appendCriterion("name not like '%聚财%'");
		
		if ("month".equals(param)) {
			fexam.appendCriterion("DATE_FORMAT (createDate,'%Y-%m')=", statdate);
			amount = financingMapper.sumByExample("amount", fexam);
		} else if ("week".equals(param)) {
			fexam.appendCriterion("DATE_FORMAT (createDate,'%x-%v')=", statdate);
			amount = financingMapper.sumByExample("amount", fexam);
		} else {
			fexam.appendCriterion("DATE_FORMAT (createDate,'%Y')=", statdate);
			amount = financingMapper.sumByExample("amount", fexam);
		}
		return amount;
	}

	@Override
	public Integer paymentPersons(String startdate, String enddate) {
		if (startdate == null && enddate == null) {
			return financingMapper.allpaymentPersons();
		} else {
			return financingMapper.paymentPersons(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> statOperationIndex(String startDate,
			String endDate, String index) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		if ("successuser".equals(index)) {
			/*
			 * 成功交易用户数
			 */
			list = readonlyOrderMapper.countSuccessUser(startDate, endDate);
		} else if ("order".equals(index)) {
			/*
			 * 下单总数
			 */
			list = readonlyOrderMapper.countOrder(startDate, endDate);
		} else if ("successorder".equals(index)) {
			/*
			 * 成功交易订单数
			 */
			list = readonlyOrderMapper.countSuccessOrder(startDate, endDate);
		} else if ("user".equals(index)) {
			/*
			 * 提交用户数
			 */
			list = readonlyOrderMapper.countUser(startDate, endDate);
		} else if ("register".equals(index)) {
			/*
			 * 注册用户数
			 */
			list = userMapper.countRegister(startDate, endDate);
		} else if ("certifyusers".equals(index)) {
			/*
			 * 认证用户（绑卡）
			 */
			list = userMapper.countCertifyuser(startDate, endDate);
		} else if ("newsuccessuser".equals(index)) {
			/*
			 * 新增成功交易用户数
			 */
			list = readonlyOrderMapper.countNewSuccessUser(startDate, endDate);
		} else if ("sale".equals(index)) {
			/*
			 * 交易金额
			 */
			list = readonlyOrderMapper.countSale(startDate, endDate);
		} else if ("allsuccessuser".equals(index)) {
			/*
			 * 所有成功交易用户数
			 */
			list = readonlyOrderMapper.allcountSuccessUser(startDate, endDate);
		} else if ("allorder".equals(index)) {
			/*
			 * 所有下单总数
			 */
			list = readonlyOrderMapper.allcountOrder(startDate, endDate);
		} else if ("allsuccessorder".equals(index)) {
			/*
			 * 所有成功交易订单数
			 */
			list = readonlyOrderMapper.allcountSuccessOrder(startDate, endDate);
		} else if ("alluser".equals(index)) {
			/*
			 * 所有提交用户数
			 */
			list = readonlyOrderMapper.allcountUser(startDate, endDate);
		} else if ("allregister".equals(index)) {
			/*
			 * 所有注册用户数
			 */
			list = userMapper.allcountRegister(startDate, endDate);
		} else if ("allcertifyusers".equals(index)) {
			/*
			 * 所有认证用户（绑卡）
			 */
			list = userMapper.allcountCertifyuser(startDate, endDate);
		} else if ("allnewsuccessuser".equals(index)) {
			/*
			 * 所有新增成功交易用户数
			 */
			list = readonlyOrderMapper.allcountNewSuccessUser(startDate,
					endDate);
		} else {
			/*
			 * 所有交易金额
			 */
			list = readonlyOrderMapper.allcountSale(startDate, endDate);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> paygatebankStat(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.allPaygatebankStat();
		} else {
			return financingMapper.paygatebankStat(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> paygateHighAmount(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.allPaygateHighAmount();
		} else {
			return financingMapper.paygateHighAmount(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> paygateOrder(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.allPaygateOrder();
		} else {
			return financingMapper.paygateOrder(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> paygateSuccessOrder(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.allPaygateSuccessOrder();
		} else {
			return financingMapper.paygateSuccessOrder(startdate, enddate);
		}

	}

	@Override
	public boolean saveDayInventory(DayInventory dayInventory) {
		return dayInventoryMapper.insert(dayInventory) > 0;
	}

	@Override
	public Integer selectInventory(String nowdate) {
		return dayInventoryMapper.selectInventory(nowdate);
	}

	@Override
	public void deleteInventory(String nowdate) {
		dayInventoryMapper.deleteInventory(nowdate);
	}

	/**
	 * 日期创建查询由 createDate 改成 valueDate
	 */
	@Override
	public Pager<Financing> notesDealOnLastDay(Pager<Financing> pager,
			String createDate) {
		FinancingExample example = new FinancingExample();
		example.appendCriterion("str_to_date(valueDate,'%Y-%m-%d')=",
				createDate);
		example.appendCriterion("(name like" + " '%" + "银票宝" + "%'"
				+ " or name like '%票据宝%')");
		example.setOrderByClause("name, createDate");
		List<Financing> list = financingMapper.selectByExample(example, pager);
		int total = financingMapper.countByExample(example);
		pager.setList(list);
		pager.setTotalCount(total);
		return pager;
	}

	@Override
	public List<Financing> yesCheckList(Pager<Financing> pager,
			String valueDate, String paymentChannel) {
		Financing financing = new Financing();
		financing.setName(valueDate);
		financing.setPayGateCode(paymentChannel);
		return financingMapper.dailyCheckList(financing);
	}

	@Override
	@Deprecated
	public Pager<Financing> monthDealList(Pager<Financing> pager,
			String valueDate) {
		FinancingExample example = new FinancingExample();
		example.appendCriterion("str_to_date(valueDate,'%Y-%m-%d')=", valueDate);
		example.appendCriterion("(name REGEXP" + " '" + "[^华数]月享" + "')");
		example.setOrderByClause("name, valueDate");
		List<Financing> list = financingMapper.selectByExample(example, pager);
		int total = financingMapper.countByExample(example);
		pager.setList(list);
		pager.setTotalCount(total);
		return pager;
	}

	@Override
	@Deprecated
	public Pager<Financing> seasonDealList(Pager<Financing> pager,
			String valueDate) {
		FinancingExample example = new FinancingExample();
		example.appendCriterion("str_to_date(valueDate,'%Y-%m-%d')=", valueDate);
		example.appendCriterion("(name like" + " '%" + "季享" + "%')");
		example.setOrderByClause("name, createDate");
		List<Financing> list = financingMapper.selectByExample(example, pager);
		int total = financingMapper.countByExample(example);
		pager.setList(list);
		pager.setTotalCount(total);
		return pager;
	}

	@Override
	public Pager<Financing> customDealList(Pager<Financing> pager,
			String valueDate, String name) {
		FinancingExample example = new FinancingExample();
		example.appendCriterion("str_to_date(valueDate,'%Y-%m-%d')=", valueDate);
		example.appendCriterion("(name REGEXP" + " '" + name + "')");
		example.setOrderByClause("name, createDate");
		List<Financing> list = financingMapper.selectByExample(example, pager);
		int total = financingMapper.countByExample(example);
		pager.setList(list);
		pager.setTotalCount(total);
		return pager;
	}

	@Override
	public List<Financing> productName(String createDate, String[] names) {
		Map<String, Object> map = new HashMap<>();
		map.put("createDate", createDate);
		map.put("names", names);
		return financingMapper.productName(map);
	}

	@Override
	public List<Financing> allProduct(Pager<Financing> pager,
			String settlementDate) {
		Financing financing = new Financing();
		financing.setName(settlementDate);
		return financingMapper.allFinancingWithDateGroupByName(financing);

	}

	@Override
	public List<Financing> regexpDealListBySettlementDate(
			Pager<Financing> pager, String name, String... settlementDate) {
		FinancingExample example = new FinancingExample();
        String sql = "str_to_date(settlementDate,'%Y-%m-%d') in (";
        for (int i = 0; i < settlementDate.length; i++){
            if (i != 0){
                sql += " , ";
            }
            sql += " '"+settlementDate[i]+"' ";
        }
        sql += ") ";
		example.appendCriterion(sql);
		example.appendCriterion("(name REGEXP" + " '" + name + "')");
		example.setOrderByClause("name, settlementDate");
		List<Financing> list = financingMapper.selectByExample(example, pager);
		int total = financingMapper.countByExample(example);
		pager.setList(list);
		pager.setTotalCount(total);
		return pager.getList();
	}

	@Override
	public List<Map<String, Object>> gatherPayGateAmount(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.gatherAllPayGateAmount();
		} else {
			return financingMapper.gatherPayGateAmount(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> gatherPayGateHighAmount(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.gatherAllPayGateHighAmount();
		} else {
			return financingMapper.gatherPayGateHighAmount(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> gatherPayGateOrder(String startdate,
			String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.gatherAllPayGateOrder();
		} else {
			return financingMapper.gatherPayGateOrder(startdate, enddate);
		}

	}

	@Override
	public List<Map<String, Object>> gatherPayGateSuccessOrder(
			String startdate, String enddate, String param) {
		if ("all".equals(param)) {
			return financingMapper.gatherAllPayGateSuccessOrder();
		} else {
			return financingMapper
					.gatherPayGateSuccessOrder(startdate, enddate);
		}

	}

}
